var http = require('http')
  , args = process.argv.slice(2)
  , port = args.length === 0 ? 8888 : parseInt(args[0])
  , url  = require('url');

/**
 * A simple web-service that echos a hello message as JSON.
 */
http.createServer(function (req, res) {

    console.log('\n' + req.url);
    var xff = 'x-forwarded-for';
    if (req.headers[xff]) {
        console.log(xff, '=', req.headers[xff]);
    }
    var q = url.parse(req.url, true).query;
    var msg = q.msg || 'World';

	res.writeHead(200, {'Content-Type': 'application/json'});
	res.end(JSON.stringify({ resp : 'Hello, ' + msg + '!'}));

}).listen(port, '127.0.0.1');

console.log('Server running on port ' + port);
